package com.todod.common.util;

import java.util.Calendar;
import java.util.Random;

/**
 * @author todod.zhang
 */

  public class TododRandomNumber {
	private static int objectNum = 0; // How many create object!
	private String generateNumber;
	private int temp;
	private int count = 0; // recording method times;

	TododRandomNumber() {
		// System.out.println("objectNum=" + objectNum);
		objectNum = objectNum + 1;
		this.generateNumber = "";
	}

	public static TododRandomNumber getInstance() {
		return new TododRandomNumber();
	}

	public String getGenerageNumber() {
		generateNumber = "";
		count = count + 1;
		Calendar datatime = Calendar.getInstance();
		temp = datatime.get(datatime.YEAR);
		generateNumber = generateNumber + temp;
		temp = datatime.get(datatime.MONTH) + 1;
		temp(temp);
		temp = datatime.get(datatime.DAY_OF_MONTH);
		temp(temp);
		temp = datatime.get(datatime.HOUR_OF_DAY);
		temp(temp);
		temp = datatime.get(datatime.MINUTE);
		temp(temp);
		temp = datatime.get(datatime.SECOND);
		temp(temp);
		// Random random = new Random();
		// temp = random.nextInt(9999);
		temp = objectNum + count;

		// System.out.println("****** temp=" + temp);
		if (temp >= 100 && temp <= 999) {
			generateNumber = generateNumber + "0" + temp;
		}
		if (temp >= 10 && temp <= 99) {
			generateNumber = generateNumber + "00" + temp;
		}
		if (temp >= 0 && temp <= 9) {
			generateNumber = generateNumber + "000" + temp;
		}
		if (temp >= 1000 && temp <= 9999) {
			generateNumber = generateNumber + temp;
		}

		return generateNumber;
	}

	/**
	 * 按类型提供流水号
	 * 
	 * @return
	 */
	public String getNumberByType(String type) {
		generateNumber = "";
		count = count + 1;
		Calendar datatime = Calendar.getInstance();
		temp = datatime.get(datatime.YEAR);
		generateNumber = generateNumber + temp;
		temp = datatime.get(datatime.MONTH) + 1;
		temp(temp);
		temp = datatime.get(datatime.DAY_OF_MONTH);
		temp(temp);
		// temp = datatime.get(datatime.HOUR_OF_DAY);
		// temp(temp);
		// temp = datatime.get(datatime.MINUTE);
		// temp(temp);
		// temp = datatime.get(datatime.SECOND);
		// temp(temp);
		// Random random = new Random();
		// temp = random.nextInt(9999);
		temp = objectNum + count;

		// System.out.println("****** temp=" + temp);
		if (temp >= 100 && temp <= 999) {
			generateNumber = generateNumber + "0" + temp;
		}
		if (temp >= 10 && temp <= 99) {
			generateNumber = generateNumber + "00" + temp;
		}
		if (temp >= 0 && temp <= 9) {
			generateNumber = generateNumber + "000" + temp;
		}
		if (temp >= 1000 && temp <= 9999) {
			generateNumber = generateNumber + temp;
		}
		generateNumber = type + generateNumber;
		return generateNumber;
	}

	public String getShortNumber() {
		generateNumber = "";
		count = count + 1;
		Calendar datatime = Calendar.getInstance();

		temp = datatime.get(datatime.HOUR_OF_DAY);

		temp = datatime.get(datatime.MINUTE);
		temp(temp);
		temp = datatime.get(datatime.SECOND);
		temp(temp);
		// Random random = new Random();
		// temp = random.nextInt(9999);
		temp = objectNum + count;

		// System.out.println("****** temp=" + temp);
		if (temp >= 100 && temp <= 999) {
			generateNumber = generateNumber + "0" + temp;
		}
		if (temp >= 10 && temp <= 99) {
			generateNumber = generateNumber + "00" + temp;
		}
		if (temp >= 0 && temp <= 9) {
			generateNumber = generateNumber + "000" + temp;
		}
		if (temp >= 1000 && temp <= 9999) {
			generateNumber = generateNumber + temp;
		}
		return generateNumber;
	}

	/**
	 * 得到随机的6位数
	 * 
	 * @return
	 */
	public String getSixNumber() {
		int randomI = (int) ((Math.random() * 9 + 1) * 100000);
		return randomI + "";
	}

	private void temp(int temp) {
		if (temp < 10) {
			generateNumber = generateNumber + "0" + temp;
		} else {
			generateNumber = generateNumber + temp;
		}
	}

	/**
	 * 得到随即数
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public int NextInt(final int min, final int max) {
		Random rand = new Random();
		int tmp = Math.abs(rand.nextInt());
		return tmp % (max - min + 1) + min;
	}

	/**
	 * 获得6位数
	 * 
	 * @return
	 */
	public String getSix(int fromNumber) {

		if (fromNumber >= 0 && fromNumber <= 9) {
			generateNumber = generateNumber + "00000" + fromNumber;
		}
		if (fromNumber >= 10 && fromNumber <= 99) {
			generateNumber = generateNumber + "0000" + fromNumber;
		}

		if (fromNumber >= 100 && fromNumber <= 999) {
			generateNumber = generateNumber + "000" + fromNumber;
		}
		if (fromNumber >= 1000 && fromNumber <= 9999) {
			generateNumber = generateNumber + "00" + fromNumber;
		}
		if (fromNumber >= 10000 && fromNumber <= 99999) {
			generateNumber = generateNumber + "0" + fromNumber;
		}

		return generateNumber;
	}

	/**
	 * 获得3位数
	 * 
	 * @return
	 */
	public String getThree(int fromNumber) {

		if (fromNumber >= 0 && fromNumber <= 9) {
			generateNumber = "00" + fromNumber;
		}
		if (fromNumber >= 10 && fromNumber <= 99) {
			generateNumber = "0" + fromNumber;
		}

		return generateNumber;
	}

	public static void main(String[] argc) {
		// int sn = TododRandomNumber.getInstance().NextInt(10000001, 99999999);

		// String aa =
		// ";00000?+99999999991101066666660000000077000000400010558398?";
		// String cardCode = aa.substring(18, 30);
		System.out.println("bb==="
				+ TododRandomNumber.getInstance().getNumberByType("RK"));
		System.out.println((int) ((Math.random() * 9 + 1) * 100000));
		// String cardVersion = aa.substring(50, 58);
		// System.out.println("bb2===" + cardVersion);
		//
		// String quan = "5";
		// System.out.println("bb2===" + quan.indexOf("-"));

	}
}
